<script setup>
import { RouterLink, RouterView } from 'vue-router'
import { ref } from 'vue'

const NAV_BAR = ref([
//   {
//     name: 'Home',
//     path: '/',
//     lis: [
//       {
//         name: 'Home1',
//         path: '/Home1',
//       },
//     ]
//   },
//   {
//     name: 'About',
//     path: '/About',
//   },
//   {
//     name: 'Contact',
//     path: '/Contact',
//   },
//   {
//     name: 'Join',
//     path: '/Join',
//   },
])

// 计算当前激活的导航项
const route = useRoute()
const activePath = computed(() => route.path)

// 添加移动端菜单状态
const isMenuOpen = ref(false)

// 切换菜单显示/隐藏
const toggleMenu = () => {
  isMenuOpen.value = !isMenuOpen.value
}

// 点击菜单项后关闭菜单（移动端）
const handleNavClick = () => {
  if (window.innerWidth <= 768) {
    isMenuOpen.value = false
  }
}
</script>

<template>
  <header>
    <div class="header-container">
      <div class="logo">WWWork</div>
      <div class="header-wrapper" :class="{ 'menu-open': isMenuOpen }">
        <nav v-for="(nav, index) in NAV_BAR" :key="index" :class="{ active: activePath === nav.path }"
          @click="handleNavClick">
          <RouterLink :to='nav.path'>{{ nav.name }}</RouterLink>
          <ul>
            <li v-for="(li, index) in nav.lis" :key="index">{{ li.name }}</li>
          </ul>
        </nav>
      </div>
      <div class="more" :class="{ 'open': isMenuOpen }" @click="toggleMenu">
        <div class="hamburger">
          <span></span>
          <span></span>
          <span></span>
        </div>
      </div>
    </div>
  </header>
</template>

<style lang="scss" scoped>
@use '@/assets/variables.scss' as *;

header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-color);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;

  .header-container {
    width: 1200px;
    height: 80px;
    display: flex;
    align-items: center;

    .more {
      width: 30px;
      height: 30px;
      display: none;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      position: relative;
    }
  }
}

.logo {
  width: 150px;
  font-size: 24px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-wrapper {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 80px;

  nav {
    position: relative;
    height: 100%;
    display: flex;
    align-items: center;

    a {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 16px 30px;
      color: inherit;
    }

    ul {
      position: absolute;
      left: 0;
      top: 100%;
      width: 100%;
      visibility: hidden;
      transform: translateY(-2px);
      opacity: 0;
      transition: all 0.3s ease-in-out;

      li {
        border-bottom: 1px solid #000;
        height: 48px;
        width: 100%;
        background: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }

    &:hover {
      cursor: pointer;
      background-color: rgba(0, 0, 0, 0.2);

      &>ul {
        visibility: visible;
        opacity: 1;
        transform: translateY(0);

        li {
          &:hover {
            background-color: rgba(0, 0, 0, 0.8);
            color: #fff;
          }
        }
      }
    }

    &.active {
      background-color: rgba(0, 0, 0, 0.2);
    }
  }
}

@media (max-width: 768px) {
  .header-container {
    position: relative;
    justify-content: space-between;
    width: 100%;
    padding: 0 20px;

    .more {
      width: 100px;
      display: flex !important;

      .hamburger {
        width: 24px;
        height: 18px;
        position: relative;
        transform: rotate(0deg);
        transition: .5s ease-in-out;

        span {
          display: block;
          position: absolute;
          height: 2px;
          width: 100%;
          background: #333;
          border-radius: 2px;
          opacity: 1;
          left: 0;
          transform: rotate(0deg);
          transition: .25s ease-in-out;

          &:nth-child(1) {
            top: 0px;
          }

          &:nth-child(2) {
            top: 8px;
          }

          &:nth-child(3) {
            top: 16px;
          }
        }
      }

      // 打开状态（变成叉号）
      &.open {
        .hamburger {
          span {
            &:nth-child(1) {
              top: 8px;
              transform: rotate(135deg);
            }

            &:nth-child(2) {
              opacity: 0;
              left: -60px;
            }

            &:nth-child(3) {
              top: 8px;
              transform: rotate(-135deg);
            }
          }
        }
      }
    }
  }

  .header-wrapper {
    position: absolute;
    top: 100%;
    left: 0;
    margin: 0;
    width: 100%;
    height: 0;
    flex-direction: column;
    background: var(--bg-color);
    overflow: hidden;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

    // 菜单打开时的状态
    &.menu-open {
      height: auto;
      max-height: 80vh; // 限制最大高度
      overflow-y: auto; // 允许滚动
    }

    nav {
      width: 100%;
      border-bottom: 1px solid rgba(0, 0, 0, 0.1);

      a {
        justify-content: flex-start;
        padding: 0 20px;
        height: 60px;
      }

      // 移动端下拉菜单样式
      ul {}

      &.active {
        background-color: rgba(0, 0, 0, 0.1);
      }

      &:hover {
        background-color: rgba(0, 0, 0, 0.05);
      }
    }
  }
}
</style>